package turing.forum.web.controller.forum;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import turing.forum.common.controller.BaseController;
import turing.forum.common.domain.AjaxResult;
import turing.forum.repository.po.User;
import turing.forum.service.IUserService;

@RestController
@RequestMapping("/forum/user")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;

    /**
     * /forum/user/login
     * @param user
     * @return
     */
    @GetMapping("/login")
    public AjaxResult login(User user) {
        User loginUser = userService.login(user, user.getPassword());
        if (loginUser == null) {
            AjaxResult.error("can not identify a user, check your username or password please");
        }
        loginUser.setPassword("*******");
        return AjaxResult.success(loginUser);
    }

    /**
     * @param user
     * @return
     */
    @GetMapping("/register")
    public AjaxResult register(User user) {
        User registerUser = userService.register(user, user.getPassword());
        if (registerUser == null) {
            AjaxResult.error("the email has already be used");
        }
        registerUser.setPassword("*******");
        return AjaxResult.success(registerUser);
    }
}
